package com.yuexunit.finance.system.action.v1.poi;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;

import com.google.inject.Singleton;
import com.yuexunit.finance.member.action.v1.EnterpriseActionApi;
import com.yuexunit.finance.member.action.v1.model.EnterpriseDetail;
import com.yuexunit.finance.project.action.v1.FinanceSignatureActionApi;
import com.yuexunit.finance.project.action.v1.model.output.SignatureDetail;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;
import com.yuexunit.web.annotation.ActionServlet;
import com.yuexunit.web.annotation.Permission;

/**
 *@version 创建时间：2017年6月27日 下午2:29:56
 *@author wenwenlong
 *类说明
 */

@ActionServlet({"/api/[^/]+/getPoiDoc.json"})
@Singleton
@Permission
public class PoiDocAction extends HttpServlet{

	private static final long serialVersionUID = 1L;

	private FinanceSignatureActionApi financeSignatureActionApi = ComponentFactory.loadClass(FinanceSignatureActionApi.class);
	private EnterpriseActionApi enterpriseActionApi = ComponentFactory.loadClass(EnterpriseActionApi.class);

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		try {
			Long enterpriseId = Long.valueOf(request.getParameter("enterpriseId"));
			EnterpriseDetail enterprise = enterpriseActionApi.inquireEnterpriseDetail(enterpriseId);
			
			ByteArrayOutputStream out1 = createAcknowledgement(enterprise);	
			ByteArrayOutputStream out2 = createAuthenticationInfo(enterprise);	
			ByteArrayOutputStream out3 = createOfficalSeal(enterprise);//企业印章	
			ByteArrayOutputStream out4 = createLegalPersonSeal(enterprise);//法人印章
			ByteArrayOutputStream out5 = createFinancialSeal(enterprise);//财务印章
			ByteArrayOutputStream out6 = createOtherSeal(enterprise);//其他印章

			//合并PDF
			ByteArrayOutputStream outPdf = new ByteArrayOutputStream();
			PDFMergerUtility ut = new PDFMergerUtility();
			if (out1.size() > 0)
				ut.addSource(convertOutPutStream2InputStream(out1));
			if (out2.size() > 0)
				ut.addSource(convertOutPutStream2InputStream(out2));
			if (out3.size() > 0)
				ut.addSource(convertOutPutStream2InputStream(out3));
			if (out4.size() > 0)
				ut.addSource(convertOutPutStream2InputStream(out4));
			if (out5.size() > 0)
				ut.addSource(convertOutPutStream2InputStream(out5));
			if (out6.size() > 0)
				ut.addSource(convertOutPutStream2InputStream(out6));
//			ut.setDestinationFileName("承诺函"+DateUtil.todayInt());
			ut.setDestinationStream(outPdf);
			ut.mergeDocuments();

			OutputStream out = response.getOutputStream();
			byte[] b = outPdf.toByteArray();

			response.setContentLength((int) b.length);  
			response.setHeader("Content-Disposition", "attachment;filename="+new String(("承诺函-" + enterprise.getEnterpriseName() + "-" + DateUtil.todayInt() + ".pdf").getBytes("GB2312"), "ISO_8859_1"));// 设置在下载框默认显示的文件名  
			response.setContentType("application/octet-stream");// 指明response的返回对象是文件流  
			out.write(b);

			out.close();
			outPdf.close();
			out1.close();
			out2.close();
			out3.close();
			out4.close();
			out5.close();
			out6.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}

	private InputStream convertOutPutStream2InputStream(ByteArrayOutputStream out ){
		InputStream in = null;
		try{
			in = new ByteArrayInputStream(out.toByteArray());
		}catch(Exception e){
			e.printStackTrace();
		}
		return in;

	}

	private ByteArrayOutputStream createAcknowledgement(EnterpriseDetail enterprise) throws Exception {
		//设置参数
		Map<String, Object> params = new HashMap<String, Object>();

		SignatureDetail sd1 = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 1);
		SignatureDetail sd2 = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 2);
		SignatureDetail sd3 = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 3);
		SignatureDetail sd4 = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 4);

		//		默认空白图片
		String firstBase64 = "iVBORw0KGgoAAAANSUhEUgAAAKkAAAC8CAYAAAADzo4HAAACC0lEQVR4nO3SsQEAEADAMPz/Mz4w6pBc0KFzXwPC1u8AeDEpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYl7wB4agV0Yca6qwAAAABJRU5ErkJggg==";
		String secondBase64 = "iVBORw0KGgoAAAANSUhEUgAAAKkAAAC8CAYAAAADzo4HAAACC0lEQVR4n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JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYlz6TkmZQ8k5JnUvJMSp5JyTMpeSYl7wB4agV0Yca6qwAAAABJRU5ErkJggg==";
		if (null != sd1 && StringUtil.isNotBlank(sd1.getData()) && sd1.getAuditState() == 2) {
			firstBase64 = sd1.getData();
		}
		if (null != sd2 && StringUtil.isNotBlank(sd2.getData()) && sd2.getAuditState() == 2) {
			secondBase64 = sd2.getData();
		}
		if (null != sd3 && StringUtil.isNotBlank(sd3.getData()) && sd3.getAuditState() == 2) {
			thirdBase64 = sd3.getData();
		}
		if (null != sd4 && StringUtil.isNotBlank(sd4.getData()) && sd4.getAuditState() == 2) {
			fourthBase64 = sd4.getData();
		}

		//设置文字替换
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String time=format.format(date);
		params.put("{company}", enterprise.getEnterpriseName());
		params.put("{date}", time);

		//设置图片替换
		//String imgFile = "F:\\eclipse\\workspace\\poidoc\\resource\\yz.png";
		//第一张
		Map<String,Object> first = new HashMap<String, Object>(); 
		first.put("width", 95); 
		first.put("height", 95); 
		first.put("type", "jpg"); 
		first.put("content", Base64Util.decodeBase64(firstBase64)); 
		//first.put("content", WordUtil.inputStream2ByteArray(new FileInputStream(imgFile), true));
		if (null == sd1 || StringUtil.isBlank(sd1.getData()) || sd1.getAuditState() != 2) {
			params.put("{first}",""); 
		} else {
			params.put("{first}",first); 
		}
		//第二张
		Map<String,Object> second = new HashMap<String, Object>(); 
		second.put("width", 159); 
		second.put("height", 159); 
		second.put("type", "jpg"); 
		second.put("content", Base64Util.decodeBase64(secondBase64)); 
		if (null == sd2 || StringUtil.isBlank(sd2.getData()) || sd2.getAuditState() != 2) {
			params.put("{second}","");
		} else {
			params.put("{second}",second);
		}
		//第三张
		Map<String,Object> third = new HashMap<String, Object>(); 
		third.put("width", 159); 
		third.put("height", 159); 
		third.put("type", "jpg"); 	
		third.put("content", Base64Util.decodeBase64(thirdBase64)); 
		if (null == sd3 || StringUtil.isBlank(sd3.getData()) || sd3.getAuditState() != 2) {
			params.put("{third}","");
		} else {
			params.put("{third}",third);
		}
		//第四张
		Map<String,Object> fourth = new HashMap<String, Object>(); 
		fourth.put("width", 159); 
		fourth.put("height", 159); 
		fourth.put("type", "jpg"); 
		fourth.put("content", Base64Util.decodeBase64(fourthBase64)); 
		if (null == sd4 || StringUtil.isBlank(sd4.getData()) || sd4.getAuditState() != 2) {
			params.put("{fourth}","");
		} else {
			params.put("{fourth}",fourth);
		}

		//打开文件
		String filePath = "C:\\Finance\\upload\\update\\template承诺函.docx";
		CustomXWPFDocument doc = WordUtil.generateWord(params, filePath); 
		//FileOutputStream fopts = new FileOutputStream("F:\\eclipse\\workspace\\poidoc\\resource\\承诺函.docx"); 
		ByteArrayOutputStream docxOS = new ByteArrayOutputStream();
		doc.write(docxOS); 

		PdfOptions options = PdfOptions.create();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PdfConverter.getInstance().convert(doc, out, options);

		docxOS.close();
		return out;
	}

	private ByteArrayOutputStream createAuthenticationInfo(EnterpriseDetail enterprise) throws IOException {
		//设置参数
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("{unitTypeId}", enterprise.getUnitTypeView());
		if ("0".equals(enterprise.getScardType())) {
			params.put("{scardType}", "普通营业执照（存在独立的组织机构代码证）");
		} else {
			params.put("{scardType}", "多证合一营业执照（不存在独立的组织机构代码证）");
		}

		params.put("{enterpriseName}", enterprise.getEnterpriseName());
		params.put("{mobile}", enterprise.getMobile());
		params.put("{codeUSC}", enterprise.getCodeUSC());
		params.put("{address}", enterprise.getAddress());
		params.put("{contactAddress}", enterprise.getContactAddress());
		params.put("{businessScope}", enterprise.getBusinessScope());
		params.put("{registeredCapital}", enterprise.getRegisteredCapital());
		if ("0".equals(enterprise.getBusinessTimeLimit())) {
			params.put("{businessTimeLimit}", "长期");
		} else {
			params.put("{businessTimeLimit}", enterprise.getBusinessTimeLimit());
		}
		params.put("{province}", enterprise.getProvince());
		params.put("{city}", enterprise.getCity());
		params.put("{district}", enterprise.getDistrict());
		params.put("{businessType}", enterprise.getBusinessType());
		params.put("{enterpriseCode}", enterprise.getBelongsEnterpriseCode());
		params.put("{sUserType}", enterprise.getsUserType());
		params.put("{creditCard}", enterprise.getCreditCard());
		params.put("{accountHolder}", enterprise.getAccountHolder());
		params.put("{depositBank}", enterprise.getDepositBank());
		params.put("{subbranch}", enterprise.getSubbranch());
		
		params.put("{legalPerson}", enterprise.getLegalPerson());
		params.put("{legalIdcard}", enterprise.getLegalIdcard());
		params.put("{legalPeriod}", enterprise.getLegalPeriod());
		params.put("{legalAreaView}", enterprise.getLegalAreaView());
		params.put("{procurator}", enterprise.getProcurator());
		params.put("{procuratorIdcard}", enterprise.getProcuratorIdcard());
		params.put("{procuratorPeriod}", enterprise.getProcuratorPeriod());

		//打开文件
		String filePath = "C:\\Finance\\upload\\update\\template企业认证信息.docx";
		CustomXWPFDocument doc = WordUtil.generateWord(params, filePath); 
		ByteArrayOutputStream docxOS = new ByteArrayOutputStream();
		doc.write(docxOS); 


		PdfOptions options = PdfOptions.create();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PdfConverter.getInstance().convert(doc, out, options);

		docxOS.close();
		return out;
	}

	private ByteArrayOutputStream createOfficalSeal(EnterpriseDetail enterprise) throws Exception {
		SignatureDetail sd = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 2);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		if (sd != null && StringUtil.isNotBlank(sd.getData()) && sd.getAuditState() == 2) {
			//设置参数
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("{enterpriseName}", sd.getEnterpriseName());
			params.put("{mobile}", enterprise.getMobile());
			params.put("{sealType}", "企业印章");
			if (2 == sd.getModelType()) {
				params.put("{modelType}", "图片印章");
			} else {
				params.put("{modelType}", "模板印章");
			}
			params.put("{enabled}", "启用");
			params.put("{hText}", sd.gethText());
			params.put("{qText}", sd.getqText());
			Map<String,Object> data = new HashMap<String, Object>(); 
			data.put("width", 159); 
			data.put("height", 159); 
			data.put("type", "jpg"); 
			data.put("content", Base64Util.decodeBase64(sd.getData())); 
			params.put("{data}", data);
			if (sd.getSubmitDateTime() == null) {
				params.put("{submitDateTime}", "0000-00-00");
			} else {
				params.put("{submitDateTime}", DateUtil.formatDate(sd.getSubmitDateTime()));
			}
			params.put("{mobileCode}", "已验证");
			params.put("{auditState}", sd.getAuditStateView());
			if (sd.getAuditDateTime() == null) {
				params.put("{auditDateTime}", "0000-00-00");
			} else {
				params.put("{auditDateTime}", DateUtil.formatDate(sd.getAuditDateTime()));
			}

			//打开文件
			String filePath = "C:\\Finance\\upload\\update\\template制章：xx印章.docx";
			CustomXWPFDocument doc = WordUtil.generateWord(params, filePath); 
			//FileOutputStream fopts = new FileOutputStream("F:\\eclipse\\workspace\\poidoc\\resource\\承诺函.docx"); 
			ByteArrayOutputStream docxOS = new ByteArrayOutputStream();
			doc.write(docxOS); 

			PdfOptions options = PdfOptions.create();

			PdfConverter.getInstance().convert(doc, out, options);

			docxOS.close();
		}
		return out;
	}

	private ByteArrayOutputStream createLegalPersonSeal(EnterpriseDetail enterprise) throws Exception {
		SignatureDetail sd = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 1);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		if (sd != null && StringUtil.isNotBlank(sd.getData()) && sd.getAuditState() == 2) {
			//设置参数
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("{enterpriseName}", sd.getEnterpriseName());
			params.put("{mobile}", enterprise.getMobile());
			params.put("{sealType}", "法人印章");
			if (2 == sd.getModelType()) {
				params.put("{modelType}", "图片印章");
			} else {
				params.put("{modelType}", "模板印章");
			}
			params.put("{enabled}", "启用");
			params.put("{hText}", sd.gethText());
			params.put("{qText}", sd.getqText());
			Map<String,Object> data = new HashMap<String, Object>(); 
			data.put("width", 95); 
			data.put("height", 95); 
			data.put("type", "jpg"); 
			data.put("content", Base64Util.decodeBase64(sd.getData())); 
			params.put("{data}", data);
			if (sd.getSubmitDateTime() == null) {
				params.put("{submitDateTime}", "0000-00-00");
			} else {
				params.put("{submitDateTime}", DateUtil.formatDate(sd.getSubmitDateTime()));
			}
			params.put("{mobileCode}", "已验证");
			params.put("{auditState}", sd.getAuditStateView());
			if (sd.getAuditDateTime() == null) {
				params.put("{auditDateTime}", "0000-00-00");
			} else {
				params.put("{auditDateTime}", DateUtil.formatDate(sd.getAuditDateTime()));
			}

			//打开文件
			String filePath = "C:\\Finance\\upload\\update\\template制章：xx印章.docx";
			CustomXWPFDocument doc = WordUtil.generateWord(params, filePath); 
			//FileOutputStream fopts = new FileOutputStream("F:\\eclipse\\workspace\\poidoc\\resource\\承诺函.docx"); 
			ByteArrayOutputStream docxOS = new ByteArrayOutputStream();
			doc.write(docxOS); 

			PdfOptions options = PdfOptions.create();
			PdfConverter.getInstance().convert(doc, out, options);

			docxOS.close();
		}
		return out;
	}

	private ByteArrayOutputStream createFinancialSeal(EnterpriseDetail enterprise) throws Exception {
		SignatureDetail sd = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 3);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		if (sd != null && StringUtil.isNotBlank(sd.getData()) && sd.getAuditState() == 2) {
			//设置参数
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("{enterpriseName}", sd.getEnterpriseName());
			params.put("{mobile}", enterprise.getMobile());
			params.put("{sealType}", "财务印章");
			if (2 == sd.getModelType()) {
				params.put("{modelType}", "图片印章");
			} else {
				params.put("{modelType}", "模板印章");
			}
			params.put("{enabled}", "启用");
			params.put("{hText}", sd.gethText());
			params.put("{qText}", sd.getqText());
			Map<String,Object> data = new HashMap<String, Object>(); 
			data.put("width", 159); 
			data.put("height", 159); 
			data.put("type", "jpg"); 
			data.put("content", Base64Util.decodeBase64(sd.getData())); 
			params.put("{data}", data);
			if (sd.getSubmitDateTime() == null) {
				params.put("{submitDateTime}", "0000-00-00");
			} else {
				params.put("{submitDateTime}", DateUtil.formatDate(sd.getSubmitDateTime()));
			}
			params.put("{mobileCode}", "已验证");
			params.put("{auditState}", sd.getAuditStateView());
			if (sd.getAuditDateTime() == null) {
				params.put("{auditDateTime}", "0000-00-00");
			} else {
				params.put("{auditDateTime}", DateUtil.formatDate(sd.getAuditDateTime()));
			}

			//打开文件
			String filePath = "C:\\Finance\\upload\\update\\template制章：xx印章.docx";
			CustomXWPFDocument doc = WordUtil.generateWord(params, filePath); 
			//FileOutputStream fopts = new FileOutputStream("F:\\eclipse\\workspace\\poidoc\\resource\\承诺函.docx"); 
			ByteArrayOutputStream docxOS = new ByteArrayOutputStream();
			doc.write(docxOS); 

			PdfOptions options = PdfOptions.create();
			PdfConverter.getInstance().convert(doc, out, options);

			docxOS.close();
		}
		return out;
	}

	private ByteArrayOutputStream createOtherSeal(EnterpriseDetail enterprise) throws Exception {
		SignatureDetail sd = financeSignatureActionApi.inquireSealDetail(enterprise.getEnterpriseId(), null, 4);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		if (sd != null && StringUtil.isNotBlank(sd.getData()) && sd.getAuditState() == 2) {
			//设置参数
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("{enterpriseName}", sd.getEnterpriseName());
			params.put("{mobile}", enterprise.getMobile());
			params.put("{sealType}", "其他印章");
			if (2 == sd.getModelType()) {
				params.put("{modelType}", "图片印章");
			} else {
				params.put("{modelType}", "模板印章");
			}
			params.put("{enabled}", "启用");
			params.put("{hText}", sd.gethText());
			params.put("{qText}", sd.getqText());
			Map<String,Object> data = new HashMap<String, Object>(); 
			data.put("width", 159); 
			data.put("height", 159); 
			data.put("type", "jpg"); 
			data.put("content", Base64Util.decodeBase64(sd.getData())); 
			params.put("{data}", data);
			if (sd.getSubmitDateTime() == null) {
				params.put("{submitDateTime}", "0000-00-00");
			} else {
				params.put("{submitDateTime}", DateUtil.formatDate(sd.getSubmitDateTime()));
			}
			params.put("{mobileCode}", "已验证");
			params.put("{auditState}", sd.getAuditStateView());
			if (sd.getAuditDateTime() == null) {
				params.put("{auditDateTime}", "0000-00-00");
			} else {
				params.put("{auditDateTime}", DateUtil.formatDate(sd.getAuditDateTime()));
			}

			//打开文件
			String filePath = "C:\\Finance\\upload\\update\\template制章：xx印章.docx";
			CustomXWPFDocument doc = WordUtil.generateWord(params, filePath); 
			//FileOutputStream fopts = new FileOutputStream("F:\\eclipse\\workspace\\poidoc\\resource\\承诺函.docx"); 
			ByteArrayOutputStream docxOS = new ByteArrayOutputStream();
			doc.write(docxOS); 

			PdfOptions options = PdfOptions.create();
			PdfConverter.getInstance().convert(doc, out, options);

			docxOS.close();
		}
		return out;
	}
}
